package li.kaeppe.travel.tracker.service;

import java.util.List;

import li.kaeppe.travel.tracker.dao.TripDao;
import li.kaeppe.travel.tracker.dao.WayPointDao;
import li.kaeppe.travel.tracker.domain.Trip;
import li.kaeppe.travel.tracker.domain.WayPoint;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class TripServiceImpl implements TripService {
	
	
	/* **************************************************** */
	/*                      CONSTANTS                       */
	/* **************************************************** */

	private static final Log log = LogFactory
			.getLog(TripServiceImpl.class);

	/* **************************************************** */
	/*                     MEMBER FIELDS                    */
	/* **************************************************** */
	
	private WayPointDao wayPointDao;
	
	private TripDao tripDao;

	/* **************************************************** */
	/*                      CONSTRUCTOR                     */
	/* **************************************************** */

	/* **************************************************** */
	/*                    PUBLIC METHODS                    */
	/* **************************************************** */
	
	public List<WayPoint> getWayPoints(long tripId) {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> getWayPoints(" + tripId + ")");
		}
		
		List<WayPoint> points = this.getWayPointDao().getAllWayPoints(tripId);
		
		if( (log.isDebugEnabled()) && 
			(points != null) ) {
			log.debug("TravelTrackerServiceImpl -> getWayPoints(): " +
					  "found " + points.size() + " records for trip [" +
					  tripId + "]");
		}
		
		return points;
	}
	
	public List<WayPoint> getWayPoints(String tripKey, int page) {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> getWayPoints(" + tripKey + ")");
		}
		
		List<WayPoint> points = this.getWayPointDao().getPagedWayPoints(tripKey, page);
		
		if( (log.isDebugEnabled()) && 
			(points != null) ) {
			log.debug("TravelTrackerServiceImpl -> getWayPoints(): " +
					  "found " + points.size() + " records for trip [" +
					  tripKey + "]");
		}
		
		return points;
	}
	
//	public List<WayPoint> getWayPoints(Trip trip) {
//		if(log.isDebugEnabled()) {
//			log.debug("TravelTrackerServiceImpl -> getWayPoints()");
//		}
//		
//		List<WayPoint> points = this.getWayPointDao().getAllWayPoints(trip);
//		
//		if( (log.isDebugEnabled()) && 
//			(points != null) ) {
//			log.debug("TravelTrackerServiceImpl -> getWayPoints(): " +
//					  "found " + points.size() + " records for trip [" +
//					  trip.toString() + "]");
//		}
//		
//		return points;
//	}
	
	public List<Trip> getAllTrips() {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> getAllTrips()");
		}
		
		List<Trip> trips = this.getTripDao().getAllTrips();
		
		if( (log.isDebugEnabled()) && 
			(trips != null) ) {
			log.debug("TravelTrackerServiceImpl -> getAllTrips(): " +
					  "found " + trips.size() + " records");
		}
		
		return trips;
	}

	public Trip storeTrip(Trip trip) {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> storeTrip()");
		}
		return this.getTripDao().storeTrip(trip);
	}
	
	public Trip getTripById(long id) {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> getTripById(id=" + id + ")");
		}
		return this.getTripDao().getTripById(id);
	}

	public Trip getTripByKey(String key) {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> getTripByKey(key=" + key + ")");
		}
		return this.getTripDao().getTripByKey(key);
	}

	public List<WayPoint> getPagedWayPoints(String tripKey, int pageNumber, int level) {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> getPagedWayPoints(key=" + tripKey + ", page=" + pageNumber + ")");
		}
		
		List<WayPoint> list = this.getWayPointDao().getPagedWayPoints(tripKey, pageNumber, level); 
		return list;
	}
	
	public WayPoint getWayPointById(long wayPointId) {
		if(log.isDebugEnabled()) {
			log.debug("TravelTrackerServiceImpl -> getWayPointById(id=" + wayPointId + ")");
		}
		
		return this.getWayPointDao().getWayPointById(wayPointId);
	}
	
	/* **************************************************** */
	/*                  PROTECTED METHODS                   */
	/* **************************************************** */

	/* **************************************************** */
	/*                   PRIVATE METHODS                    */
	/* **************************************************** */

	/* **************************************************** */
	/*                  GETTERS AND SETTERS                 */
	/* **************************************************** */

	public TripDao getTripDao() {
		return tripDao;
	}

	public void setTripDao(TripDao tripDao) {
		this.tripDao = tripDao;
	}

	public WayPointDao getWayPointDao() {
		return wayPointDao;
	}

	public void setWayPointDao(WayPointDao wayPointDao) {
		this.wayPointDao = wayPointDao;
	}

}
